
import 'package:flutter/material.dart';
import 'package:flutterlib/common/net/dao_result.dart';
import 'package:flutterlib/common/net/retrofit/post_api.dart';
import 'package:flutterlib/common/utils/toast.dart';
import 'package:flutterlib/common/utils/utils.dart';
import 'package:flutterlib/model/post/post_bean.dart';
import 'package:flutterlib/pages/post/post_list_widget_page.dart';
import 'package:flutterlib/widgets/bussiness/post/post_list_item.dart';
import 'package:flutterlib/widgets/dialog/heart_dialog.dart';

class PostSearchListWidgetPage extends PostListWidgetPage {

  final String searchWord;

  PostSearchListWidgetPage({Key key, this.searchWord}): super(key: key);

  @override
  PostSearchListWidgetPageState createState() => PostSearchListWidgetPageState();
}

///页面 KeepAlive ，同时支持 动画Ticker
class PostSearchListWidgetPageState extends PostListWidgetPageState<PostSearchListWidgetPage>
    {
      String word;

  getDataLogic(int page) async {
    print(page);

    trueDebugPrint("flutterting searchpage getdatalogic");
    if(widget.searchWord.isEmpty){
      return new DataResult([], true);
    }
    var posts = await PostApi.builder().searchByQuery(
        page - 1, 20, "createTime,desc", widget.searchWord
    );
    return new DataResult(posts.content, true);
  }

  @override
  void didUpdateWidget(PostSearchListWidgetPage oldWidget) {
    super.didUpdateWidget(oldWidget);
    trueDebugPrint("flutterting searchpage didupdatewidget");
  }


  Widget renderItem(index, post) {
    //提交
    // var post = pullLoadWidgetControl.dataList[index];
    return PostListItem(post, onPressed: () {
      // NavigatorUtils.goPostDetail(context, 6, name : widget.name);
    }, onZhanPressed: (isLike)async{
      PostBean listItem = manger.getListItem(index);
      if(!listItem.isLiked){
        var likeBean = await PostApi.builder().like(listItem.id);
        manger.notifyDataSetChange((){
          listItem.likes = listItem.likes + 1;
          listItem.isLiked = true;
        });
        if(likeBean != null && likeBean.match){
          HeartDialog.show(
            context,
            text: '互赞加为好友',
          );
        }
      }else{
        await PostApi.builder().unLike(listItem.id);
        manger.notifyDataSetChange((){
          listItem.likes = listItem.likes - 1;
          listItem.isLiked = false;
        });
      }
      return !isLike;
    }, onCommentPressed: (){

    }, onCollectPressed: (){

      PostBean listItem = manger.getListItem(index);
      if(!listItem.isCollect){
        PostApi.builder().collect(listItem.id);
        manger.notifyDataSetChange((){
          listItem.isCollect = true;
        });
      }else{
        PostApi.builder().unCollect(listItem.id);
        manger.notifyDataSetChange((){
          listItem.isCollect = false;
        });
      }
    },);
  }

  @override
  List<Widget> renderHeader(BuildContext context, bool innerBoxIsScrolled) {
    List<Widget> widgets = [];

    return widgets;
  }

}
